// 列车定位：根据定位数据定位列车位置
import * as THREE from 'three';
import gsap from 'gsap';

import { keyBy } from 'lodash';

import posSegments from './定位区段数据（全景车站）.json';

// 定位区段
type PosSegment = {
  code: string;
  start: [number, number, number];
  end: [number, number, number];
  actualLength: number;
  isBesideStation?: boolean;
  stationOffset?: number;
};

// 设置 ---------------------------------------------------------------------------------

// 列车模型名称
const targetTrainModelName = 'Train_LieChe';

// 列车的 Y 坐标（升降位置）
const trainPosOffset = new THREE.Vector3(0, 1.4, 0);

// 每次定位的时间间隔
const interval = 3;
// 进出站动画移动距离
const trainEnterExitStationDist = 30;

// 测试数据 -----------------------------------------------------------------------------

// 区段数据
const segmentCodeMap = keyBy(posSegments as PosSegment[], 'code');

// 正线
// T3201A: 21142 ~ 21266
let testInputs: any = [
  // { segmentCode: 'T3201A', offset: 0 },
  // { segmentCode: 'T3201A', offset: 120 },
  // { segmentCode: 'T3201A', offset: 240 },
  // { segmentCode: 'T3203A', offset: 106.3 }, // 长沙火车南站-北侧站台
  // { segmentCode: 'T3211B', offset: 0 },
];

// 停车场
// 41G: 300 ~ 419
// const testInputs = [
//   { segmentCode: '41G', offset: 0 },
//   { segmentCode: '41G', offset: 30 },
//   { segmentCode: '41G', offset: 60 },
//   { segmentCode: '41G', offset: 90 },
//   { segmentCode: '41G', offset: 120 },
//   { segmentCode: '7/33G', offset: 0 },
//   { segmentCode: '7/33G', offset: 50 },
//   { segmentCode: '7/33G', offset: 100 },
//   { segmentCode: '7/33G', offset: 150 },
// ]

// 模拟 ---------------------------------------------------------------------------------

let currentPositionIndex = -1;
let timer = -1;

const _start = new THREE.Vector3();
const _end = new THREE.Vector3();
const _vec = new THREE.Vector3();
const _pos = new THREE.Vector3();
let isLastSegBesideStation = false; // 上一个位置是否在车站旁边
let lastTrainPosX = 0;

function nextPosition() {
  currentPositionIndex = (currentPositionIndex + 1) % testInputs.length;

  const currentPosData = testInputs[currentPositionIndex];
  const currentSegmentData = segmentCodeMap[currentPosData.segmentCode];
  if (!currentSegmentData) {
    console.error(`列车定位：找不到区段“${currentPosData.segmentCode}”`);
  } else {
    _start.fromArray(currentSegmentData.start);
    _end.fromArray(currentSegmentData.end);
    _vec.copy(_end).sub(_start).setLength(currentPosData.offset);
    _pos.copy(_start).add(_vec);
    const rotY = -Math.atan2(_end.z - _start.z, _end.x - _start.x);
    if (trainModel) {
      trainModel.position.copy(_pos);
      trainModel.rotation.y = rotY;
    }
  }
  stopSimPositioning();
  // if (isSimPositioningEnabled) {
  //   timer = window.setTimeout(() => nextPosition(), 1000);
  // }
}

// API ----------------------------------------------------------------------------------

export let isSimPositioningEnabled = true; // 模型定位已开启
// 开始模拟定位
export function startSimPositioning(group: THREE.Object3D, position: {}) {
  trainModel = group;
  testInputs = [position];
  if (trainModel == null) alert(`未找到列车模型“${targetTrainModelName}”`);
  if (testInputs.length == 0) return;
  else nextPosition();
}

// 停止模拟定位
export function stopSimPositioning() {
  clearTimeout(timer);
  isSimPositioningEnabled = false;
}

// 列车模型物体
let trainModel: THREE.Object3D | null = null;
